package com.ants.boot.wx.service.impl;

import com.ants.boot.core.security.utils.SecurityUtils;
import com.ants.boot.wx.dao.WxArticleCommentMapper;
import com.ants.boot.wx.domain.WxArticleComment;
import com.ants.boot.wx.dto.wxarticlecomment.AddCommentDto;
import com.ants.boot.wx.service.WxArticleCommentService;
import com.ants.boot.wx.vo.wxarticlecomment.WxArticleCommentVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 蚂蚁会花呗
 * @date 2022/6/7 14:55
 */
@Service
public class WxArticleCommentServiceImpl extends ServiceImpl<WxArticleCommentMapper, WxArticleComment> implements WxArticleCommentService {

    @Autowired
    private WxArticleCommentMapper wxArticleCommentMapper;

    @Override
    public List<WxArticleCommentVo> listComment(Long articleId) {
        return wxArticleCommentMapper.listComment(articleId);
    }

    @Override
    @Transactional
    public boolean addComment(AddCommentDto dto) {
        WxArticleComment comment = new WxArticleComment();
        BeanUtils.copyProperties(dto,comment);
        comment.setUserId(SecurityUtils.getLoginUser().getUserId());
        comment.setCommentTime(LocalDateTime.now());
        return this.save(comment);
    }

    @Override
    @Transactional
    public Long getIdAddComment(AddCommentDto dto) {
        WxArticleComment comment = new WxArticleComment();
        BeanUtils.copyProperties(dto,comment);
        comment.setUserId(SecurityUtils.getLoginUser().getUserId());
        comment.setCommentTime(LocalDateTime.now());
        this.save(comment);
        return comment.getId();
    }
}
